﻿using System;
using System.Collections.Generic;
using System.Windows.Media.Imaging;
using System.IO;

namespace SlideShowGenerator.Data
{
    /// <summary>
    /// 
    /// </summary>
    public class ImageData
    {

        /// <summary>  </summary>
        public string Title { get; set; }
        /// <summary>  </summary>
        public string ImageFile { get; set; }
        /// <summary>  </summary>
        public string ThumbFile { get; set; }
        /// <summary>  </summary>
        public string DateTaken { get; set; }
        /// <summary>  </summary>
        public string Comment { get; set; }
        /// <summary>  </summary>
        public string Author { get; set; }
        private AlbumData album;
        private string fileBaseName;
        private bool enableGeneration = true;

        /// <summary>  </summary>
        public AlbumData Album
        {
            get { return album; }
            set { album = value; }
        }

        /// <summary>  </summary>
        public bool EnableGeneration
        {
            get { return enableGeneration; }
            set { enableGeneration = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        public ImageData(string path)
        {
            BitmapSource img = BitmapFrame.Create(new Uri(path));
            BitmapMetadata meta = (BitmapMetadata)img.Metadata; 

            if (!string.IsNullOrEmpty(meta.DateTaken))
            {
                //Just get the date, not the time (i usually forget to adjust my camera when traveling abroad)
                string[] date = meta.DateTaken.Split(' ');
                if (date.Length > 0)
                {
                    this.DateTaken = date[0];
                }
            }
            
            // Get the title and comment from the photo
            this.Title = meta.Title;
            this.Comment = meta.Comment;
            // If both title and comment are blank, don't automatically select this image for output to the album
            if (string.IsNullOrEmpty(this.Comment) && string.IsNullOrEmpty(this.Title))
            {
                this.enableGeneration = false;
            }
            if (meta.Author != null && meta.Author.Count > 0)
            {
                this.Author = meta.Author[0];
            }         

            FileInfo file = new FileInfo(path);
            fileBaseName = file.Name.Remove(file.Name.ToUpper().IndexOf(".JPG"), 4);
            this.ImageFile = path;
        }

        /// <summary>  </summary>
        public string FileBaseName
        {
            get { return fileBaseName; }
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class AlbumData
    {
        /// <summary>  </summary>
        public string Title { get; set; }
        /// <summary>  </summary>
        public string Description { get; set; }
        /// <summary>  </summary>
        public string ThumbImage { get; set; }
        /// <summary>  </summary>
        public string ThumbImagePath { get; set; }
        private bool enableGeneration = true;
        private List<ImageData> images = new List<ImageData>();

        /// <summary>  </summary>
        public bool EnableGeneration
        {
            get { return enableGeneration; }
            set { enableGeneration = value; }
        }

        /// <summary>  </summary>
        public List<ImageData> Images
        {
            get {  return images;  }
        }
    }
}
